package com.scqzy.shengchunqiang.sword2offer.day11;

import com.scqzy.shengchunqiang.structure.ListNode;

import java.util.Objects;

/**
 * @Description: 剑指 Offer 22. 链表中倒数第k个节点
 * @Author 盛春强
 * @Date 2021/8/18 15:22
 */
public class GetKthFromEnd {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k && Objects.nonNull(fast); i++) {
            fast = fast.next;
        }
        while (Objects.nonNull(fast)) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
